package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author majun
 * @package sky-take-out
 * @date 2024/5/17 : 上午8:32
 */

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //判断商品是否存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {

            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.upDataByNumber(shoppingCart1);

        } else {

            //如果不存在 插入数据

            //判断添加的是套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                //添加为菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //为套餐
                Setmeal setmeal = setmealMapper.getFromById(String.valueOf(setmealId));
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> list() {

        //获取用户id
        Long userid = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userid)
                .build();

        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        // 封装删除或更新购物车项的逻辑
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        updateOrDeleteCartItems(list);
    }

    /**
     * 清空购物车
     */
    public void clean() {

        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.deteleUserId(userId);

    }

    /**
     * 删除商品逻辑
     *
     * @param cartItems
     */
    private void updateOrDeleteCartItems(List<ShoppingCart> cartItems) {
        if (cartItems != null) {
            for (ShoppingCart cartItem : cartItems) {
                Integer number = cartItem.getNumber();
                if (number == null || number > 1) {
                    // 数量大于1 执行数量减1
                    cartItem.setNumber(number - 1);
                    shoppingCartMapper.upDataByNumber(cartItem);
                } else {
                    // 数量等于1 删除商品
                    shoppingCartMapper.detele(cartItem.getId());
                }
            }
        }
    }

}